package net.oschina.j2cache.redis;

import net.oschina.j2cache.Cache;
import net.oschina.j2cache.CacheException;
import net.oschina.j2cache.CacheExpiredListener;
import net.oschina.j2cache.CacheProvider;
import redis.clients.jedis.BinaryJedis;

/**
 * Redis 缓存实现
 * @author Winter Lau
 */
public class RedisCacheProvider implements CacheProvider {


	@Override
	public Cache buildCache(String regionName, boolean autoCreate, CacheExpiredListener listener) throws CacheException {
		try {
            BinaryJedis jedis = RedisPool.me().getResource();
            RedisCache cache = new RedisCache(regionName, jedis);
			return cache;
		} catch (Exception e) {
			throw new CacheException("Unabled to build redis cache,name="+regionName, e);
		}
	}

	@Override
	public void start() throws CacheException {
	}

	@Override
	public void stop() {
		RedisPool.me().destroy();
	}

}
